package com.wzw.expression.expression.invocation;

import com.wzw.expression.expression.operation.Operation;
import lombok.Getter;
import org.springframework.aop.support.AopUtils;
import org.springframework.context.expression.AnnotatedElementKey;
import org.springframework.core.BridgeMethodResolver;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 代理运行过程中固定的方法及属性缓存
 *
 * @author Wangzhiwen
 */
@Getter
public abstract class OperationInvocationMetadata implements Serializable {
    private final Operation operation;

    private final Method method;

    private final Class<?> targetClass;

    private final Method targetMethod;

    private final AnnotatedElementKey methodKey;

    public OperationInvocationMetadata(Operation operation, Method method, Class<?> targetClass) {
        this.operation = operation;
        this.method = BridgeMethodResolver.findBridgedMethod(method);
        this.targetClass = targetClass;
        this.targetMethod = (!Proxy.isProxyClass(targetClass) ? AopUtils.getMostSpecificMethod(method, targetClass) : this.method);
        this.methodKey = new AnnotatedElementKey(this.targetMethod, targetClass);
    }
}
